package com.zb.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zb.dao.BaseDao;
import com.zb.dao.UserDao;
import com.zb.entity.Car;
import com.zb.entity.User;

public class UserDaoImpl implements UserDao{
	private BaseDao db=new BaseDao();
	public List<User> getUserList(Integer index, Integer size) {
		List<User> list=new ArrayList<User>();
		String sql="select * from user";
		
		sql+=" order by Id limit ?,?";
		
		ResultSet rs=db.query(sql, (index-1)*size,size);
		try {
			while(rs.next()) {
				User u=new User();
				u.setId(rs.getInt(1));
				u.setName(rs.getString(2));
				u.setSex(rs.getString(3));
				u.setTel(rs.getString(4));
				u.setEmail(rs.getString(5));
				Car c=new Car();
				c.setId(rs.getInt(6));
				u.setDay(rs.getString(7));
				u.setAddress(rs.getString(8));
				u.setScore(rs.getInt(9));
				u.setBalance(rs.getInt(10));
				u.setLoginName(rs.getString(11));
				u.setLoginPwd(rs.getString(12));
				u.setCar(c);
				list.add(u);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return list;
	}

	public int getUserCount() {
		String sql="SELECT COUNT(*) FROM `user`";
		
		
		ResultSet rs=db.query(sql);
		
		int count=0;
		try {
			if(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return count;
	}

	public void addUser(User u) {
		String sql="insert user into values(null,?,?,?,?,?,NOW(),?,?,?,?,?)";
		db.update(sql, u.getName(),u.getSex(),u.getTel(),u.getEmail(),u.getCar().getId(),
				u.getAddress(),u.getScore(),u.getBalance(),u.getLoginName(),u.getLoginPwd());
		db.close();
	}

	public void update(User u) {
		String sql="update user set  Name=?,Sex=?,Tel=?,Mail=?,Carid=?,day=NOW(),Address=?,Score=?,Money=?,LoginName=?,LoginPwd=? where Id=?";
		db.update(sql, u.getName(),u.getSex(),u.getTel(),u.getEmail(),u.getCar().getId(),
				u.getAddress(),u.getScore(),u.getBalance(),u.getLoginName(),u.getLoginPwd(),u.getId());
		db.close();
	}

	public void delUser(int id) {
		String sql="delete from user where id=?";
		db.update(sql, id);
		db.close();
		
	}

	public User getUserById(int id) {
		String sql="select * from user where id=?";
		ResultSet rs=db.query(sql, id);
		try {
			if(rs.next()) {
				User u=new User();
				u.setId(rs.getInt(1));
				u.setName(rs.getString(2));
				u.setSex(rs.getString(3));
				u.setTel(rs.getString(4));
				u.setEmail(rs.getString(5));
				Car c=new Car();
				c.setId(rs.getInt(6));
				u.setDay(rs.getString(7));
				u.setAddress(rs.getString(8));
				u.setScore(rs.getInt(9));
				u.setBalance(rs.getInt(10));
				u.setLoginName(rs.getString(11));
				u.setLoginPwd(rs.getString(12));
				u.setCar(c);
				return u;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return null;
	}

	

}
